package Controller

import (
	"back_go/DAO"
	models "back_go/DAO/model"
	"back_go/Response"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-xorm/xorm"
	"strconv"
	"time"
)

// GetAreaList 获取地区列表
/*
	参数：无
	@return 地区列表
*/
func GetAreaList(c *gin.Context) {

	var engine = DAO.Engine
	var areaList []models.TzArea
	err := engine.Find(&areaList)
	if err != nil {
		c.JSON(200, Response.ResponseBody{
			Code:    "A00005",
			Msg:     "服务出了小差错",
			Data:    nil,
			Version: "v1.0",
			Sign:    nil,
			Success: false,
			Fail:    true,
		})
		return
	}
	//middleware.Cacher.Set("GetAreaList", areaList, cache.DefaultExpiration)
	c.JSON(200, Response.ResponseBody{
		Code:      "00000",
		Msg:       nil,
		Data:      areaList,
		Version:   "v1.0",
		Timestamp: time.Now().Unix(),
		Sign:      nil,
		Success:   true,
		Fail:      false,
	})

}

// GetAreaById 通过id获取地区信息
/*
	参数：area_id
	@return 地区信息
*/
func GetAreaById(c *gin.Context) {
	var engine = DAO.Engine
	id := c.Param("area_id")
	fmt.Println(id)
	var area models.TzArea
	_, err := engine.Where("area_id = ?", id).Get(&area)

	if err != nil {
		fmt.Println(err)
		c.JSON(200, Response.ResponseBody{
			Code:    "A00005",
			Msg:     "服务出了小差错",
			Data:    nil,
			Version: "v1.0",
			Sign:    nil,
			Success: false,
			Fail:    true,
		})
		return
	}
	c.JSON(200, Response.ResponseBody{
		Code:      "00000",
		Msg:       nil,
		Data:      area,
		Version:   "v1.0",
		Timestamp: time.Now().Unix(),
		Sign:      nil,
		Success:   true,
	})
}

// GetAreaByPid 根据pid获取地区信息
/*
	参数：pid
	@return 地区信息
*/
func GetAreaByPid(c *gin.Context) {

	var engine = DAO.Engine
	var areaList []models.TzArea
	pid := c.Query("pid")
	err := engine.Where("parent_id = ?", pid).Find(&areaList)
	if err != nil {
		c.JSON(200, Response.ResponseBody{
			Code:    "A00005",
			Msg:     "服务出了小差错",
			Data:    nil,
			Version: "v1.0",
			Sign:    nil,
			Success: false,
			Fail:    true,
		})
		return
	}
	c.JSON(200, Response.ResponseBody{
		Code:      "00000",
		Msg:       nil,
		Data:      areaList,
		Version:   "v1.0",
		Timestamp: time.Now().Unix(),
		Sign:      nil,
		Success:   true,
	})
}

// AddArea 添加地区
/*
	参数：area_name,parent_id
	@return 地区id
*/
func AddArea(c *gin.Context) {
	var engine = DAO.Engine
	var area models.TzArea
	err := c.ShouldBindJSON(&area)
	if area.ParentId != 0 {
		var area1 models.TzArea
		_, _ = engine.Where("area_id = ?", area.ParentId).Get(&area1)
		area.Level = area1.Level + 1
	}
	_, err = engine.Insert(&area)
	if err != nil {
		c.JSON(200, Response.ResponseBody{
			Code:    "A00005",
			Msg:     "服务出了小差错",
			Data:    nil,
			Version: "v1.0",
			Sign:    nil,
			Success: false,
			Fail:    true,
		})
		return
	}
	c.JSON(200, Response.ResponseBody{
		Code:      "00000",
		Msg:       nil,
		Data:      area,
		Version:   "v1.0",
		Timestamp: time.Now().Unix(),
		Sign:      nil,
		Success:   true,
	})
}

// UpdateArea 修改地区信息
/*
	参数：area_id,area_name,parent_id
	@return 地区id
*/
func UpdateArea(c *gin.Context) {
	var engine = DAO.Engine
	var area models.TzArea
	_ = c.ShouldBindJSON(&area)
	areaDb := models.TzArea{}
	// 获取原地区信息
	_, _ = engine.ID(area.AreaId).Get(&areaDb)

	// 判断当前省市区级别，如果是 1 级、2 级则不能修改级别，不能修改成别人的下级
	if areaDb.Level == 1 && area.Level != 1 {
		c.JSON(200, Response.ResponseBody{
			Code:    "A00002",
			Msg:     "不能改变一级行政地区的级别",
			Data:    nil,
			Version: "v1.0",
			Sign:    nil,
			Success: false,
			Fail:    true,
		})
		return
	}
	if areaDb.Level == 2 && area.Level != 2 {
		c.JSON(200, Response.ResponseBody{
			Code:    "A00002",
			Msg:     "不能改变二级行政地区的级别",
			Data:    nil,
			Version: "v1.0",
			Sign:    nil,
			Success: false,
			Fail:    true,
		})
		return
	}
	// 检查是否有同名地区
	if hasSameName(area, engine) {
		c.JSON(200, Response.ResponseBody{
			Code:      "A00002",
			Msg:       "存在同名地区",
			Data:      nil,
			Version:   "v1.0",
			Sign:      nil,
			Fail:      true,
			Success:   false,
			Timestamp: time.Now().Unix(),
		})
		return
	}
	// 更新地区信息
	_, _ = engine.ID(area.AreaId).Update(&area)
	c.JSON(200, Response.ResponseBody{
		Code:      "00000",
		Msg:       "ok",
		Data:      nil,
		Version:   "v1.0",
		Timestamp: time.Now().Unix(),
		Success:   true,
		Fail:      false,
		Sign:      nil,
	})

}

// hasSameName 检查是否有同名地区
/*
	参数：area,engine
	@return bool
*/
func hasSameName(area models.TzArea, engine *xorm.Engine) bool {
	count, err := engine.Where("parent_id =?", area.ParentId).And("area_name =?", area.AreaName).NotIn("area_id", area.AreaId).Count(new(models.TzArea))
	if err != nil {
		// 处理错误，如果需要可以记录日志
		return false
	}
	return count > 0
}

// DeleteArea 删除地区
/*
	参数：area_id
	@return 地区id
*/
func DeleteArea(c *gin.Context) {
	var engine = DAO.Engine
	areaid := c.Param("area_id")
	id, _ := strconv.ParseInt(areaid, 10, 64)
	_, err := engine.Delete(&models.TzArea{AreaId: id})
	if err != nil {
		c.JSON(200, Response.ResponseBody{
			Code:    "A00002",
			Msg:     "删除失败",
			Data:    nil,
			Version: "v1.0",
			Sign:    nil,
			Success: false,
			Fail:    true,
		})
		return
	}
	c.JSON(200, Response.ResponseBody{
		Code:      "00000",
		Msg:       "ok",
		Data:      nil,
		Version:   "v1.0",
		Timestamp: time.Now().Unix(),
		Success:   true,
		Sign:      nil,
	})
}
